package com.wex.mycppapp.slice;

import com.wex.jni.JniFFmpeg;
import com.wex.mycppapp.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;

import com.wex.jni.JniTest;

public class MainAbilitySlice extends AbilitySlice {

    private TextField txtConsole = null;
    private Button btnFun1 = null;
    private Button btnFun2 = null;
    private Button btnFun3 = null;
    private Button btnFun4 = null;
    private Button btnFun5 = null;

    private long fdEncode = 0;
    private long fdDecode = 0;

    private int width = 1920;
    private int height = 1080;
    private int fps = 30;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_main_ability_slice_layout);

        txtConsole = (TextField)findComponentById(ResourceTable.Id_main_slice_txt_console);
        btnFun1 = (Button) findComponentById(ResourceTable.Id_main_slice_btn_fun1);
        btnFun2 = (Button) findComponentById(ResourceTable.Id_main_slice_btn_fun2);
        btnFun3 = (Button) findComponentById(ResourceTable.Id_main_slice_btn_fun3);
        btnFun4 = (Button) findComponentById(ResourceTable.Id_main_slice_btn_fun4);
        btnFun5 = (Button) findComponentById(ResourceTable.Id_main_slice_btn_fun5);

        btnFun1.setClickedListener((component) -> txtConsole.append("testjniHello(): " + JniTest.testjniHello() + "\r\n"));
        btnFun2.setClickedListener((component) -> txtConsole.append("testjniFun(1, 2): " + JniTest.testjniFun(1, 2) + "\r\n"));
        btnFun3.setClickedListener((component) -> txtConsole.append("ffjniInfo(): " + JniFFmpeg.ffjniInfo() + "\r\n"));
        btnFun4.setClickedListener((component) -> {
            int ret = JniFFmpeg.codecPush(fdEncode, new byte[width * height * 3 / 2]);
            if (ret != 0)
                txtConsole.append("encode codecPush: failed " + ret + "\r\n");
            else {
                byte[] retBytes = JniFFmpeg.codecGain(fdEncode);
                if (retBytes != null) {
                    txtConsole.append("encode codecGain: " + retBytes.length + ": "
                            + retBytes[0] + " " + retBytes[1] + " " + retBytes[2] + " " + retBytes[3] + " " + retBytes[4] + " " + retBytes[5] + "\r\n");
                }
            }
        });
        btnFun5.setClickedListener((component) -> {
            int ret = JniFFmpeg.codecPush(fdDecode, new byte[10240]);
            if (ret != 0)
                txtConsole.append("decode codecPush: failed " + ret + "\r\n");
            else {
                byte[] retBytes = JniFFmpeg.codecGain(fdDecode);
                if (retBytes != null) {
                    txtConsole.append("decode codecGain: " + retBytes.length + ": "
                            + retBytes[0] + " " + retBytes[1] + " " + retBytes[2] + " " + retBytes[3] + " " + retBytes[4] + " " + retBytes[5] + "\r\n");
                }
            }
        });

        fdEncode = JniFFmpeg.codecOpen(JniFFmpeg.Type_Encode, JniFFmpeg.Format_H264, width, height, fps);
        fdDecode = JniFFmpeg.codecOpen(JniFFmpeg.Type_Decode, JniFFmpeg.Format_H264, width, height, fps);
        txtConsole.append("fd: " + fdEncode + " / " + fdDecode + "\n");
    }

    @Override
    public void onStop() {
        super.onStop();
        JniFFmpeg.codecClose(fdEncode);
        JniFFmpeg.codecClose(fdDecode);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
